package com.xinfu.newproject.model.complaint;

import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.complaint.bean.ComplaintDetailBean;
import com.xinfu.newproject.bean.complaint.bean.ComplaintEvaluateBean;
import com.xinfu.newproject.bean.complaint.bean.ComplaintRecordBean;
import com.xinfu.newproject.bean.complaint.bean.SubmitComplaintBean;
import com.xinfu.newproject.bean.complaint.body.ComplaintDetailBody;
import com.xinfu.newproject.bean.complaint.body.ComplaintEvaluateBody;
import com.xinfu.newproject.bean.complaint.body.ComplaintRecordBody;
import com.xinfu.newproject.bean.complaint.body.SubComplaintBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ComplaintModel implements IModel.IComplaintModel {

    /**
     * 提交投诉
     *
     * @param
     * @param callBack
     */
    @Override
    public void submitComplaint(SubComplaintBody subComplaintBody, MyCallBack<SubmitComplaintBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", subComplaintBody.getUserId());
        map.put("type", subComplaintBody.getType());
        map.put("name", subComplaintBody.getName());
        map.put("telephone", subComplaintBody.getTelephone());
        map.put("content", subComplaintBody.getContent());
        map.put("projectId", subComplaintBody.getProjectId());
        map.put("tenantId", subComplaintBody.getTenantId());
        map.put("picture", subComplaintBody.getPicture());

        JsNetworkService.getInstance().getIJsNetworkService()
                .submitComplaint(subComplaintBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SubmitComplaintBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull SubmitComplaintBean submitComplaintBean) {

                        callBack.onSuccess(submitComplaintBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 投诉记录
     *
     * @param complaintRecordBody
     * @param callBack
     */
    @Override
    public void complaintRecord(ComplaintRecordBody complaintRecordBody, MyCallBack<ComplaintRecordBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", complaintRecordBody.getUserId());


        JsNetworkService.getInstance().getIJsNetworkService()
                .complaintRecord(complaintRecordBody.getHeader(), complaintRecordBody.getUserId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ComplaintRecordBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull ComplaintRecordBean complaintRecordBean) {

                        callBack.onSuccess(complaintRecordBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 投诉详情
     *
     * @param complaintDetailBody
     * @param callBack
     */
    @Override
    public void complaintDetail(ComplaintDetailBody complaintDetailBody, MyCallBack<ComplaintDetailBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("complaintId", complaintDetailBody.getComplaintId());

        JsNetworkService.getInstance().getIJsNetworkService()
                .complaintDetail(complaintDetailBody.getHeader(), complaintDetailBody.getComplaintId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ComplaintDetailBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull ComplaintDetailBean complaintDetailBean) {

                        callBack.onSuccess(complaintDetailBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void complaintEvaluate(ComplaintEvaluateBody complaintEvaluateBody, MyCallBack<ComplaintEvaluateBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", complaintEvaluateBody.getContent());
        map.put("imageUrl", complaintEvaluateBody.getImageUrl());
        map.put("userName", complaintEvaluateBody.getUserName());
        map.put("tenantId", complaintEvaluateBody.getTenantId());
        map.put("status", complaintEvaluateBody.getStatus());
        map.put("id", complaintEvaluateBody.getId());

        JsNetworkService.getInstance().getIJsNetworkService()
                .complaintEvaluate(complaintEvaluateBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ComplaintEvaluateBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull ComplaintEvaluateBean complaintEvaluateBean) {

                        callBack.onSuccess(complaintEvaluateBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
